using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using AAM.BIZ;
using System.Collections;
using CMS.BOP;

namespace CMS.BIZ
{
    public class BizPage : SiteMapNode, IBizObject
    {

        public static readonly ObjectType ObjectType = new ObjectType("BizPage");

        private ObjectID id;

        private int pageID = 0;
        private int? parentID = null;
        private string pageTitle = "";
        private int pageIndex = 0;
        private string masterPageFile = "";
        private int? contentID = null;
        private bool requireAuthentication = false;

        public BizPage(SiteMapProvider provider, int pageID, string name, int? parentID, string description, IList roles)
            : base(provider, pageID.ToString(), "?Page=" + pageID.ToString(), name, description, roles, null, null, null)
        {
            id = new ObjectID(ObjectType, new object[] { pageID });
            this.pageID = pageID;
            this.parentID = parentID;
        }

        #region IBizObject Members

        public ObjectID ID
        {
            get { return id; }
        }

        public ObjectType Type
        {
            get { return ID.Type; }
        }

        public string TypeName
        {
            get { return Type.Name; }
        }

        public int PageID
        {
            get { return pageID; }
            set { base.Url = "?Page=" + value.ToString(); ID[0] = value; pageID = value; }
        }

        public int? ParentID
        {
            get { return parentID; }
            set { parentID = value; }
        }

        public string PageTitle
        {
            get { return pageTitle; }
            set { pageTitle = value; }
        }

        public int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        public string PageName
        {
            get { return base.Title; }
            set { base.Title = value; }
        }

        public string MasterPageFile
        {
            get { return masterPageFile; }
            set { masterPageFile = value; }
        }

        public int? ContentID
        {
            get { return contentID; }
            set { contentID = value; }
        }

        public bool RequireAuthentication
        {
            get { return requireAuthentication; }
            set { requireAuthentication = value; }
        }

        #endregion

        public override bool Equals(object obj)
        {
            if (!(obj is IBizObject)) return false;
            IBizObject other = (IBizObject)obj;
            return id.Equals(other.ID);
        }

        public override int GetHashCode()
        {
            return id.GetHashCode();
        }

        public override string ToString()
        {
            return id.ToString();
        }

        public string AuthorizedRolesString
        {
            get
            {
                StringBuilder builder = new StringBuilder();
                bool add = false;
                foreach (string str in Roles)
                {
                    if (add) builder.Append(",");
                    else add = true;
                    builder.Append(str);
                }
                return builder.ToString();
            }
        }
    }
}
